/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.controller;

import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.manager.entity.IPCC_EXTENSION_GROUPS;
import com.ipcc.manager.entity.PARTNER;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@ViewScoped
public class IPCC_EXTENSION_GROUPSController implements Serializable{
    //
    private List<IPCC_EXTENSION_GROUPS> listIEG = new ArrayList<IPCC_EXTENSION_GROUPS>();
    private LazyDataModel<IPCC_EXTENSION_GROUPS> lazyIEGModel;
    private List<PARTNER> listPartner = new ArrayList<PARTNER>();
    //
    private IPCC_EXTENSION_GROUPS currentIEG = new IPCC_EXTENSION_GROUPS();
    
    /**
     * Creates a new instance of IPCC_EXTENSION_GROUPSController
     */
    public IPCC_EXTENSION_GROUPSController() {
        try {
            listIEG = IPCC_EXTENSION_GROUPS.getAll("");
            lazyIEGModel = new LazyAbstractDataModel<IPCC_EXTENSION_GROUPS>(listIEG);
            listPartner = PARTNER.getAll("");
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + ex.getMessage()));
        }
    }

    public LazyDataModel<IPCC_EXTENSION_GROUPS> getLazyIEGModel() {
        return lazyIEGModel;
    }

    public void setLazyIEGModel(LazyDataModel<IPCC_EXTENSION_GROUPS> lazyIEGModel) {
        this.lazyIEGModel = lazyIEGModel;
    }

    public List<PARTNER> getListPartner() {
        return listPartner;
    }

    public void setListPartner(List<PARTNER> listPartner) {
        this.listPartner = listPartner;
    }

    public List<IPCC_EXTENSION_GROUPS> getListIEG() {
        return listIEG;
    }

    public void setListIEG(List<IPCC_EXTENSION_GROUPS> listIEG) {
        this.listIEG = listIEG;
    }

    public IPCC_EXTENSION_GROUPS getCurrentIEG() {
        return currentIEG;
    }

    public void setCurrentIEG(IPCC_EXTENSION_GROUPS currentIEG) {
        this.currentIEG = currentIEG;
    }

    public String insert(){
        try {
            if(currentIEG.insert()){
                listIEG.add(currentIEG);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thành công!"));
                lazyIEGModel = new LazyAbstractDataModel<IPCC_EXTENSION_GROUPS>(listIEG);                
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Thêm bản ghi thất bại!"));            
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + ex.getMessage()));
        } finally{
            currentIEG = new IPCC_EXTENSION_GROUPS();
        }        
        return "";
    }
    
    public String remove(IPCC_EXTENSION_GROUPS ieg){
        try {
            if(ieg.remove()){
                listIEG.remove(ieg);
                lazyIEGModel = new LazyAbstractDataModel<IPCC_EXTENSION_GROUPS>(listIEG);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thành công!"));
            }
            else FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Xóa bản ghi thất bại!"));
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + ex.getMessage()));
        }
        return "";
    }
    
    public String prepareEdit(IPCC_EXTENSION_GROUPS ieg){
        try{
            currentIEG = (IPCC_EXTENSION_GROUPS) ieg.clone();
        }
        catch(Exception ex){
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + ex.getMessage()));
        }
        return "";
    }
    
    public String edit(){
        try {
            if(currentIEG.edit()){
                for(int i=0;i<listIEG.size();i++){
                    if(listIEG.get(i).getGroup().equals(currentIEG.getGroup())){
                        listIEG.set(i, currentIEG);
                        break;
                    }
                }
                lazyIEGModel = new LazyAbstractDataModel<IPCC_EXTENSION_GROUPS>(listIEG);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Sửa bản ghi thành công!"));                
            }
            else{
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Sửa bản ghi thất bại!"));
            }            
        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + ex.getMessage()));
        } finally{
            currentIEG = new IPCC_EXTENSION_GROUPS();
        }       
        return "";
    }
}